﻿using Coolzon.UI.Core.Parts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Coolzon.UI.Core.Runtime.Script.Animation
{
    public class RotateAnimationScriptObject : AnimationScriptObject
    {
        private double _offsetAngle;

        public RotateAnimationScriptObject(double offsetAngle, double duration)
            : base(duration)
        {
            _offsetAngle = offsetAngle;
        }

        public override void AppendToStoryboard(Widget instance, ref double duration, Storyboard storyboard)
        {
            PropertyValueKeeper.Instance.Save(instance, Widget.RotateProperty, instance.Rotate);

            DoubleAnimation animationRotate = new DoubleAnimation();
            Storyboard.SetTarget(animationRotate, instance);
            Storyboard.SetTargetProperty(animationRotate, new System.Windows.PropertyPath(Widget.RotateProperty));
            animationRotate.BeginTime = TimeSpan.FromSeconds(duration);
            animationRotate.Duration = new System.Windows.Duration(TimeSpan.FromSeconds(Duration));
            animationRotate.From = instance.Rotate;
            animationRotate.To = animationRotate.From.Value + _offsetAngle;
            storyboard.Children.Add(animationRotate);

            duration += Duration;
        }
    }
}
